<template>
  <el-table :data="tableData" header-cell-class-name="table-header">
    <el-table-column
        prop="name"
        label="模板名称">
    </el-table-column>
    <el-table-column
        prop="createTime"
        label="创建时间">
    </el-table-column>
    <el-table-column
        prop="updateTime"
        label="更新时间">
    </el-table-column>
    <el-table-column>
      <template #header>
        <el-row justify="space-between">
          <el-col :span="3" style="margin-top: 9px">
            操作
          </el-col>
          <el-col :span="10">
            <el-button type="primary" style="margin-right: 20px" @click="handleAdd">新增模板</el-button>
          </el-col>
        </el-row>
      </template>
      <template #default="scope">
        <el-popover
            :width="200"
            trigger="hover">
          <template #reference>
            <el-button
                type="text"
                style="color: #2c3e50">预览</el-button>
          </template>
            <span v-html="scope.row.content.replace(/\n/g,'<br>').replace(/ /g,'&nbsp')"></span>
        </el-popover>
        <el-button
            type="text"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        <el-button
            type="text"
            style="color:rgb(245, 108, 108) "
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

<!--添加和修改模板的对话框-->
  <el-dialog :title="title" v-model="dialogVisible" width="40%">
    <el-form :model="form">
      <el-form-item label="模板名称">
        <el-row>
          <el-col :span="10">
            <el-input type="text" v-model="form.name"></el-input>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="模板内容">
        <el-row>
          <el-col :span="10">
            <el-input type="textarea" v-model="form.content" rows="5"></el-input>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="模板示例">
        <el-row :gutter="20">
          <el-col :span="10">
            <p style="line-height: 25px">
              收信人姓名、时间等变量使用时会根据各项目实际情况自动匹配，格式务必书写正确！<br>
              例：尊敬的学校行政部门人员${name}:<br>
              &nbsp;&nbsp;&nbsp;&nbsp;我校于${date}将会举办${activity name}培训活动，届时需要笔、笔记本、文件袋${number}份，请提前准备！
            </p>
          </el-col>
          <el-col :span="10">
            <p style="line-height: 25px">
              ${college name} = 学院名称<br>
              ${room} = 寝室/教室<br>
              ${activity name} = 活动名称<br>
              ${number} = 数量<br>
              ${name} = 收信人<br>
              ${date} = 时间
            </p>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">保 存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import axios from "axios";

export default {
  name: "TemplateTable",
  data(){
    return{
      tableData:[{
        name:'审批提醒',
        createTime:'2021-04-01 10:03:27',
        updateTime:'/',
        content:'尊敬的学校行政部门人员${name}:\n我校于${date}将会举办${activity name}培训活动，届时需要笔、笔记本、文件袋${number}份，请提前准备！'
      },{
        name:'审批提醒',
        createTime:'2021-04-01 10:03:27',
        updateTime:'/',
        content:'尊敬的学校行政部门人员${name}:我校于${date}将会举办${activity name}培训活动，届时需要笔、笔记本、文件袋${number}份，请提前准备！'
      },{
        name:'审批提醒',
        createTime:'2021-04-01 10:03:27',
        updateTime:'/',
        content:'尊敬的学校行政部门人员${name}:我校于${date}将会举办${activity name}培训活动，届时需要笔、笔记本、文件袋${number}份，请提前准备！'
      }],
      title:'',
      dialogVisible:false,
      form:{
        name:'',
        content:'',
      },
      isEdit:false,
    }
  },
  methods:{
    handleAdd(){
      this.title = '新建模板';
      this.form={
        name:'',
        content:'',
      };
      this.isEdit = false;
      this.dialogVisible = true;
    },
    handleEdit(index,row){
      this.title = '编辑模板';
      this.findMessageTemplateById(row.id).then(()=>{
        this.isEdit = true;
        this.dialogVisible = true;
      });
    },
    handleDelete(index,row){
      this.$confirm('确定删除这条信息记录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteMessageTemplateById(row.id).then(()=>{
          this.findAllMessageTemplate();
        });
      });
    },
    submit(){
      let promise;
      if(this.isEdit){
        promise = this.updateMessageTemplate(this.form);
      }else{
        promise = this.insertMessageTemplate(this.form);
      }
      this.dialogVisible = false;
      promise.then(()=>{
        this.findAllMessageTemplate();
      });
      return promise;
    },
    findAllMessageTemplate(){
      return axios.get(this.$host+"/message/template").then((response)=>{
        if(response.data.code === 0){
          this.tableData = response.data.data;
        }else{
          this.$message.error(response.data.msg);
        }
      })
    },
    findMessageTemplateById(id){
      return axios.get(this.$host+"/message/template/"+id).then((response)=>{
        if(response.data.code === 0){
          this.form = response.data.data;
        }else{
          this.$message.error(response.data.msg);
        }
      })
    },
    updateMessageTemplate(data){
      return axios.put(this.$host+"/message/template",data).then((response)=>{
        if(response.data.code === 0){
          this.$message.success("修改成功");
        }else{
          this.$message.error(response.data.msg);
        }
      })
    },
    insertMessageTemplate(data){
      return axios.post(this.$host+"/message/template",data).then((response)=>{
        if(response.data.code === 0){
          this.$message.success("添加成功");
        }else{
          this.$message.error(response.data.msg);
        }
      })
    },
    deleteMessageTemplateById(id){
      return axios.delete(this.$host+"/message/template/"+id).then((response)=>{
        if(response.data.code === 0){
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }else{
          this.$message.error(response.data.msg);
        }
      })
    }
  },
  created() {
    this.findAllMessageTemplate();
  }
}
</script>

<style scoped>

</style>